Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Mail

Module mdbFunction
    Public Function Seperate(ByVal lsX As String, ByVal lsY As String) As String
        On Error Resume Next
        Dim intStart, intLen As Int16
        intStart = InStr(1, lsX, lsY) + Len(lsY) + 1
        If Mid(lsX, intStart, 1) = "}" Then Return ""
        intLen = InStr(1, Right(lsX, Len(lsX) - InStr(1, lsX, lsY) - Len(lsY) - 1), "}")
        Return Mid(lsX, intStart, intLen)
    End Function
    Public Function doSQL(ByVal vSql As String) As Boolean
        On Error GoTo herr

        Return True
        Exit Function
herr:
        Return False
    End Function

    Public Function getsValue(ByVal vSql As String) As String
        Dim myConn As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
        Dim sOut As String
        myConn.Open()
        Dim myComm As New SqlCommand(vSql, myConn)
        sOut = myComm.ExecuteScalar
        myComm.Dispose()
        myConn.Close()
        Return sOut
    End Function
    Public Function getiValue(ByVal vSql As String) As Integer
        Dim myConn As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
        Dim sOut As Integer
        myConn.Open()
        Dim myComm As New SqlCommand(vSql, myConn)
        sOut = myComm.ExecuteScalar
        myComm.Dispose()
        myConn.Close()
        Return sOut
    End Function

    Public Function sendMail(ByVal vAddress As String, ByVal vSubject As String, ByVal vContent As String, Optional ByVal vCC As String = "") As Boolean

        Dim mailmsg As New MailMessage
        Dim objMail As SmtpMail
        objMail.SmtpServer = "mail.kdadmin.com"
        mailmsg.BodyFormat = MailFormat.Html
        mailmsg.BodyEncoding = System.Text.Encoding.UTF8
        mailmsg.From = "info@kdadmin.com"
        mailmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1")
        mailmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "info@kdadmin.com")
        mailmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "secret!@#")
        mailmsg.To = vAddress
        If vCC <> "" Then mailmsg.Cc = vCC
        mailmsg.Subject = vSubject
        mailmsg.Body = vContent
        Try
            objMail.Send(mailmsg)
        Catch ex As Exception
            Return False
        End Try
        Return True

    End Function

    Public Function emailvalidation(ByVal vEmail As String)
        Dim apos, dotpost, lastpost
        If (vEmail = "") Then
            Return False
        Else
            apos = InStr(vEmail, "@")
            dotpost = InStrRev(vEmail, ".")
            lastpost = Len(vEmail) - 1
            If (apos < 1) Or (dotpost - apos < 2) Or (lastpost - dotpost > 3) Or (lastpost - dotpost < 1) Then
                Return False
            Else
                Return True
            End If
        End If
    End Function

    Public Function emailExist(ByVal vEmail As String) As Boolean
        Dim sSql As String = "select count(*) from Khachhang where email ='" & Replace(vEmail, "'", "''") & "'"
        Dim myConn As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
        Dim MyCommand As SqlCommand
        myConn.Open()
        MyCommand = New SqlCommand(sSql, myConn)
        If MyCommand.ExecuteScalar = 1 Then
            Return True
        Else
            Return False
        End If
        myConn.Close()
    End Function
    Public Function VN2SysDate(ByVal strVNdate As String) As String
        On Error GoTo herr
        Dim strOut As String
        Dim strdate() As String
        strdate = Split(strVNdate, "/", 3)
        strOut = strdate(1) & "/" & strdate(0) & "/" & strdate(2)
        Return strOut
herr:
        strOut = "01/01/1980"
        Return strOut
    End Function
    Public Function Sys2VNDate(ByVal strVNdate As String) As String
        On Error GoTo herr
        Dim strOut As String
        Dim strdate() As String
        strdate = Split(strVNdate, "/", 3)
        strOut = strdate(1) & "/" & strdate(0) & "/" & strdate(2)
        Return strOut
herr:
        strOut = "01/01/1980"
        Return strOut
    End Function

    Public Function getRandom() As String
        Dim vRandom As Random
        vRandom = New System.Random(Date.Now.Millisecond)
        getRandom = vRandom.Next(1000, 10000).ToString
    End Function
    Public Function enCode(ByVal vPass As String) As String
        Return vPass
    End Function
    Public Function deCode(ByVal vPass As String) As String
        Return vPass
    End Function
    Public Function removeSpeKey(ByVal vString As String) As String

        Dim s As String, i As Integer
        Dim sStr As String()
        s = "';--;select;drop;insert;delete;xp_;Persits;Upload;MapPath;script;<script>;"
        sStr = Split(s, ";")
        For i = 0 To UBound(sStr) - 1
            vString = Replace(vString, sStr(i), "")
            vString = Replace(vString, UCase(sStr(i)), "")
            vString = Replace(vString, LCase(sStr(i)), "")
        Next
        If vString Is Nothing Then vString = " "
        Return vString
    End Function
    Public Function TygiaUSD(ByVal giaVN As Double) As Double
        Dim myConn As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
        Dim sSql As String = "select top 1 usd from tygia order by ngay desc"
        myConn.Open()
        Dim cmd As New SqlCommand(sSql, myConn)
        Dim tygia As Double = cmd.ExecuteScalar()
        myConn.Close()
        Return Math.Round((giaVN / tygia), 2)
    End Function
End Module
